//第一块，配置
require.config({
    baseUrl: "static/admin/lib",
    paths: {
        jquery:    "jquery-1.8.1.min",
        avalon:    "avalon/avalon.shim",
        bootstrap: "bootstrap/js/bootstrap",
        mmRouter:  "avalon/mmRouter",
        mmHistory: "avalon/mmHistory",
        text:      "avalon/text",
        domReady:  "avalon/domReady",
        css:       "avalon/css"
    },
    priority: ["text", "css"],
    shim: {
        jquery: {
            exports: "jQuery"
        },
        avalon: {
            exports: "avalon"
        },
        bootstrap: {
            exports: "jQuery"
        }
    }
});

//添加根VM（处理共用部分）
require(["avalon", "jquery", "bootstrap", "mmRouter", "domReady!"], function() {

    var model = avalon.define({
        $id: "app",
        brand: {
            first: "Dream",
            second: "Lu"
        },
        sidebarNav: true,
        user: {
            logged: true,
            nickName: 'DreamLu'
        },
        contentClass: "content",
        currentDialog: "signIn",
        toggle: function(dialog) {
            model.currentDialog = dialog;
        },
        currPath: "/admin/home.html"
    });

    avalon.router.get("/admin/sign-in.html", function() {
        model.user.logged   = false;

        model.sidebarNav    = false;
        model.contentClass  = "row-fluid";
        model.currentDialog = "signIn";
        model.currPath      = this.path;
    });

    avalon.router.get("/admin/sign-up.html", function() {
        model.user.logged   = false;

        model.sidebarNav    = false;
        model.contentClass  = "row-fluid";
        model.currentDialog = "signUp";
        model.currPath      = "/admin/sign-in.html";
    });

    avalon.router.get("/admin/reset-password.html", function() {
        model.user.logged   = false;

        model.sidebarNav    = false;
        model.contentClass  = "row-fluid";
        model.currentDialog = "resetPassword";
        model.currPath      = "/admin/sign-in.html";
    });

    avalon.router.get("/admin/index.html", callback);
    avalon.router.get("/admin/users.html", callback);
    avalon.router.get("/admin/user.html", callback);
    avalon.router.get("/admin/media.html", callback);
    avalon.router.get("/admin/calendar.html", callback);
    avalon.router.get("/admin/privacy-policy.html", callback);
    avalon.router.get("/admin/terms-and-conditions.html", callback);
    avalon.router.get("/admin/help.html", callback);
    avalon.router.get("/admin/faq.html", callback);

    avalon.router.get("/admin/404.html", callback);
    avalon.router.get("/admin/500.html", callback);

    function callback() {
        model.user.logged  = true;
        model.sidebarNav   = true;
        model.contentClass = "content";

        if (this.path === '/admin/index.html') {
            model.currPath = "/admin/home.html";
        } else {
            model.currPath = this.path;
        }
    }

    avalon.history.start({
        basepath: "",
        html5Mode: false
    });

    avalon.scan(document.body);

});